package MediaOnline.VideoPlayer;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.widget.Button;

public class NahiDialog {

	private int mChoice = 1;        
	private boolean mQuitModal = false; 
	private int waitingSecond = 7;
	private String strYes = "Có";
	private String strNo = "Không";
	private String strCancel = "Hủy";
	private AlertDialog alert;
	
	private Method mMsgQueueNextMethod = null;
	private Field mMsgTargetFiled = null;
	private Button bt = null;
	private String strButton = "";

	public NahiDialog() {
	}
	
	public void showAlertDialog(Context context, String info){
		showAlertDialog(context, 0, null, info);
	}
	
	public void showAlertDialog(Context context, int icon, String title, String info) {
	    if (!prepareModal()) {
	        return;
	    }
	
	    // build alert dialog
	    AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    if (icon != 0)
	    	builder.setIcon(icon);
	    if (!"".equals(title))
	    	builder.setTitle(title);
	    builder.setMessage(info);
	    builder.setCancelable(false);	    	    
	    builder.setPositiveButton(strYes, new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int id) {
	            NahiDialog.this.mQuitModal = true;
	            dialog.dismiss();
	        }
	    });
	
	    alert = builder.create();
	    alert.show();
	    NahiDialog.this.bt = alert.getButton(DialogInterface.BUTTON_POSITIVE);	
	    NahiDialog.this.strButton = strYes;
	    
	    if (waitingSecond !=0)
		{					
			new CountDownTimer(waitingSecond*1000, 1000) {
				public void onTick(long millisUntilFinished) {
					 
					NahiDialog.this.bt.setText(strButton + " (" + millisUntilFinished / 1000 + ")");
					NahiDialog.this.bt.invalidate();
				}
				public void onFinish() {
					bt.setText(strButton);
					bt.invalidate();	
					mQuitModal = true;					
					alert.dismiss();
				}
			}.start();
		}
	    
	    
	    // run in modal mode
	    doModal();
	}
	
	public boolean showConfirmDialog(Context context, String info) {
		return showConfirmDialog(context, 0, null, info);
	}
	public boolean showConfirmDialog(Context context, int icon, String title, String info) {
	    if (!prepareModal()) {
	        return false;
	    }
	
	    // reset choice
	    mChoice = 1;
	
	    AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    if (icon != 0)
	    	builder.setIcon(icon);
	    if (!"".equals(title))
	    	builder.setTitle(title);
	    builder.setMessage(info);
	    builder.setCancelable(false);
	    builder.setPositiveButton(strYes, new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int id) {
	            NahiDialog.this.mQuitModal = true;
	            NahiDialog.this.mChoice = 0;
	            dialog.dismiss();
	        }
	    });
	
	    builder.setNegativeButton(strNo, new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int id) {
	            NahiDialog.this.mQuitModal = true;
	            NahiDialog.this.mChoice = 1;
	            dialog.cancel();
	        }
	    });
	
	    alert = builder.create();
	    alert.show();
	    NahiDialog.this.bt = alert.getButton(DialogInterface.BUTTON_NEGATIVE);	
	    NahiDialog.this.strButton = strNo;
	    
	    if (waitingSecond !=0)
		{					
			new CountDownTimer(waitingSecond*1000, 1000) {
				public void onTick(long millisUntilFinished) {
					 
					NahiDialog.this.bt.setText(strButton + " (" + millisUntilFinished / 1000 + ")");
					NahiDialog.this.bt.invalidate();
				}
				public void onFinish() {
					bt.setText(strButton);
					bt.invalidate();	
					mQuitModal = true;					
					alert.cancel();
				}
			}.start();
		}
	    
	    doModal();
	    return mChoice == 0;
	}
	
	public int showConfirmDialogCancel(Context context, String info) {
		return showConfirmDialogCancel(context, 0, null, info);
	}
	public int showConfirmDialogCancel(Context context, int icon, String title, String info) {
	    if (!prepareModal()) {
	        return 2;
	    }
	
	    // reset choice
	    mChoice = 2;
	
	    AlertDialog.Builder builder = new AlertDialog.Builder(context);
	    if (icon != 0)
	    	builder.setIcon(icon);
	    if (!"".equals(title))
	    	builder.setTitle(title);
	    builder.setMessage(info);
	    builder.setCancelable(false);
	    builder.setPositiveButton(strYes, new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int id) {
	            NahiDialog.this.mQuitModal = true;
	            NahiDialog.this.mChoice = 0;
	            dialog.dismiss();
	        }
	    });
	
	    builder.setNeutralButton(strNo, new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int id) {
	            NahiDialog.this.mQuitModal = true;
	            NahiDialog.this.mChoice = 1;
	            dialog.cancel();
	        }
	    });
	    
	    builder.setNegativeButton(strCancel, new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int id) {
	            NahiDialog.this.mQuitModal = true;
	            NahiDialog.this.mChoice = 2;
	            dialog.cancel();
	        }
	    });
	    
	 
	    alert = builder.create();
	    alert.show();
	    NahiDialog.this.bt = alert.getButton(DialogInterface.BUTTON_NEGATIVE);	
	    NahiDialog.this.strButton = strCancel;
	    
	    if (waitingSecond !=0)
		{					
			new CountDownTimer(waitingSecond*1000, 1000) {
				public void onTick(long millisUntilFinished) {
					 
					NahiDialog.this.bt.setText(strButton + " (" + millisUntilFinished / 1000 + ")");
					NahiDialog.this.bt.invalidate();
				}
				public void onFinish() {
					bt.setText(strButton);
					bt.invalidate();	
					mQuitModal = true;					
					alert.cancel();
				}
			}.start();
		}
	    doModal();	    
	    return mChoice;
	}
	
	private boolean prepareModal() {
	    Class<?> clsMsgQueue = null;
	    Class<?> clsMessage = null;
	
	    try {
	        clsMsgQueue = Class.forName("android.os.MessageQueue");
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	        return false;
	    }
	
	    try {
	        clsMessage = Class.forName("android.os.Message");
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	        return false;
	    }
	
	    try {
	        mMsgQueueNextMethod = clsMsgQueue.getDeclaredMethod("next", new Class[]{});
	    } catch (SecurityException e) {
	        e.printStackTrace();
	        return false;
	    } catch (NoSuchMethodException e) {
	        e.printStackTrace();
	        return false;
	    }
	
	    mMsgQueueNextMethod.setAccessible(true);
	
	    try {
	        mMsgTargetFiled = clsMessage.getDeclaredField("target");
	    } catch (SecurityException e) {
	        e.printStackTrace();
	        return false;
	    } catch (NoSuchFieldException e) {
	        e.printStackTrace();
	        return false;
	    }
	
	    mMsgTargetFiled.setAccessible(true);
	    return true;
	}
	
	private void doModal() {
		mQuitModal = false;		
	    MessageQueue queue = Looper.myQueue();
	    while (!mQuitModal) {	        
	        Message msg = null;
	        try {
	            msg = (Message)mMsgQueueNextMethod.invoke(queue, new Object[]{});
	        } catch (IllegalArgumentException e) {
	            e.printStackTrace();
	        } catch (IllegalAccessException e) {
	            e.printStackTrace();
	        } catch (InvocationTargetException e) {
	            e.printStackTrace();
	        }
	
	        if (null != msg) {
	            Handler target = null;
	            try {
	                target = (Handler)mMsgTargetFiled.get(msg);
	            } catch (IllegalArgumentException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            }
	
	            if (target == null) {
	                // No target is a magic identifier for the quit message.
	                mQuitModal = true;
	            }
	
	            target.dispatchMessage(msg);
	            msg.recycle();
	        }
	    }
	}
}
